package com.mengxiangnongfu.controller.system;

import com.mengxiangnongfu.aspectj.annotation.Log;
import com.mengxiangnongfu.aspectj.enums.BusinessType;
import com.mengxiangnongfu.config.utils.ShiroSecurityUtils;
import com.mengxiangnongfu.domain.DictData;
import com.mengxiangnongfu.dto.DicDataDto;
import com.mengxiangnongfu.service.DictDataService;
import com.mengxiangnongfu.vo.AjaxResult;
import com.mengxiangnongfu.vo.DataGridView;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;

@RequestMapping("system/dic/data")
@RestController
@Api(value = "字典数据")
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    /**
     * 带条件分页查询
     *
     * @return
     */
    @GetMapping("loadDictDataByPage")
    public AjaxResult loadDictDataByPage(DicDataDto dicDataDto) {
        DataGridView dataGridView = dictDataService.loadDictDataByPage(dicDataDto);
        return AjaxResult.success("查询全部字典数据成功!", dataGridView, dataGridView.getTotalCount());
    }

    /**
     * 根据类型查询字典数据
     *
     * @param type
     * @return
     */
    @GetMapping("loadDictDataByType/{type}")
    public AjaxResult loadDictDataByType(@Validated @NotBlank(message = "字典数据类型不能为空!") @PathVariable("type") String type) {
        DataGridView dataGridView = dictDataService.loadDictDataByType(type);
        return AjaxResult.success("分页查询字典信息成功!", dataGridView, dataGridView.getTotalCount());
    }

    /**
     * 添加字典数据
     *
     * @param dicDataDto
     * @return
     */
    @Log(title = "插入字典数据", businessType = BusinessType.INSERT)
    @PostMapping("insertDicData")
    public AjaxResult insertDicData(@Validated DicDataDto dicDataDto) {
        dicDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictDataService.insertDicData(dicDataDto));
    }

    /**
     * 修改字典数据
     *
     * @param dicDataDto
     * @return
     */
    @Log(title = "修改字典数据", businessType = BusinessType.UPDATE)
    @PutMapping("modifyDicData")
    public AjaxResult modifyDicData(@Validated DicDataDto dicDataDto) {
        dicDataDto.setUpdateBy(ShiroSecurityUtils.getCurrentSimpleUser().getUserName());
        dicDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictDataService.modifyDicData(dicDataDto));
    }

    /**
     * 删除字典数据
     *
     * @param ids
     * @return
     */
    @Log(title = "删除字典数据", businessType = BusinessType.DELETE)
    @DeleteMapping("removeDicData/{ids}")
    public AjaxResult removeDicData(@PathVariable @Validated @NotBlank(message = "删除的字典数据ID不能为空!") Long[] ids) {
        Integer result = dictDataService.removeDicData(ids);
        return result.intValue() > 0 ? AjaxResult.success("删除字典数据成功,共删除" + ids.length + "条数据!") : AjaxResult.error("删除字典数据失败!");
    }

    /**
     * 根据编号查询字典数据信息
     *
     * @param id
     * @return
     */
    @GetMapping("getDictData/{id}")
    public AjaxResult loadDictDataById(@PathVariable @Validated @NotEmpty(message = "字典数据的ID不能为空!") Serializable id) {
        DictData dictData = dictDataService.loadDictDataById(id);
        return AjaxResult.success("查询成功", dictData);
    }
}
